import { CoreException, CoreExceptionType, UserExceptionType } from '@/modules'
import { timestampFormat } from '@/util'
import { Injectable, OnModuleInit } from '@nestjs/common'
import { RoleActionRepo, RoleProjectRepo } from '@/entities'
import defaultRoles from './role'
import { IBodyUser } from '@/interface'

@Injectable()
export class RoleService implements OnModuleInit {
    constructor(private readonly roleActionRepo: RoleActionRepo, private readonly roleProjectRepo: RoleProjectRepo) {}

    public async onModuleInit() {
        const exiting = await this.roleActionRepo.find({ userid: 'system' })
        if (exiting.length === 0) {
            const repos = []
            defaultRoles.map((role) => {
                repos.push({ ...role, userid: 'system', ct: timestampFormat(), au: 'system' })
            })
            await this.roleActionRepo.save(repos)
        }
    }

    /**
     * 查询我的功能权限
     * @param option
     */
    public async queryMyAction(option: IBodyUser) {
        const userid = option.__user.userid
        const data = await this.roleActionRepo.find({ userid })
        const options = []
        data.map((d) => {
            options.push({
                label: d.actionName,
                value: d.action,
            })
        })
        return options
    }

    public async queryActionListByUserid(userid: string) {
        const list = await this.queryMyAction({ __user: { userid } })
        return list.map((l) => l.value)
    }
}
